package com.controller;

import com.util.MinioUtils;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: hanll
 * @ClassName: MinioController
 * @Date: 2022-11-14 15:28
 * @Version: 1.0
 * @Description:
 */
@RestController
public class MinioController {

    @Resource
    private MinioUtils minioUtils;

    @Value("${minio.endpoint}")
    private String address;
    @Value("${minio.bucketName}")
    private String bucketName;

    @RequestMapping("/upload")
    public String upload(MultipartFile file) {
        List<String> upload = minioUtils.upload(new MultipartFile[]{file});
        return address+"/"+bucketName+"/"+upload.get(0);
    }

    @RequestMapping("/download")
    public ResponseEntity<byte[]> download(String fileName) {
        ResponseEntity<byte[]> download = minioUtils.download(fileName);
        return download;
    }


    @RequestMapping("/download/plus")
    public ResponseEntity<byte []> downloadPlus(String fileName) throws IOException {
        ResponseEntity<byte []> response = null;
        File file = new File("E:\\2896850293\\FileRecv\\"+fileName);
        InputStream inputStream = file.toURL().openStream();
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        HttpHeaders headers = new HttpHeaders();
        try {
            IOUtils.copy(inputStream, outputStream);
            byte [] bytes = outputStream.toByteArray();
            headers.add("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            headers.setAccessControlExposeHeaders(Arrays.asList("*"));
            response = new ResponseEntity<>(bytes, headers, HttpStatus.OK);
            return response;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return response;
    }

}
